package engine

import (
	"log"

	"../../crawler/fetcher"
)

func Run(seeks ...Request) {

	var requests []Request

	for _, r := range seeks {
		requests = append(requests, r)

	}

	for len(requests) > 0 {
		r := requests[0]
		requests = requests[1:]

		log.Printf("Fetching %s ", r.Url)
		body, err := fetcher.Fetch(r.Url)

		if err != nil {
			log.Printf("Fetcher : error fetching url %s : %v", r.Url, err)
			continue
		}

		parseResult := r.ParserFunc(body)
		requests = append(requests, parseResult.Requests...)

		for _, item := range parseResult.Items {
			log.Printf("Got item %v.", item)
		}
	}
}
